<!--
 * @Description: 隧道编排
 * @Author: 0001100986/LiuShuai
 * @Date: 2021-06-10 14:14:01
 * @LastEditTime: 2021-11-12 14:29:14
 * @LastEditors: 0001101008/HuYuxi
-->
<template>
  <div class="evpnoipsc-content">
    <template v-if="show === 0">
      <table-bar
        :showTop="false"
        :columns="columns"
        @changeColumn="changeColumn"
        layout="search, column"
      >
        <div slot="top">
          <el-form :model="filterSearch" label-width="80px" label-position="right">
            <el-row :gutter="20">
              <el-col :xs="24" :sm="12" :lg="6">
                <!-- 隧道名称 -->
                <el-form-item label="隧道名称">
                  <el-input v-model.trim="filterSearch.userlabel" placeholder="名称" maxlength="200"></el-input>
                </el-form-item>
              </el-col>
              <!-- 搜索 重置 -->
              <el-row :xs="24" :sm="12" :lg="6" style="float: right; margin-right: 10px;">
                <el-button type="primary" @click="search">搜索</el-button>
                <el-button @click="reset">重置</el-button>
              </el-row>
            </el-row>
          </el-form>
        </div>
        <div slot="bottom">
          <!-- 刷新 -->
          <el-button size="medium" @click="reset" v-power="2">刷新</el-button>
          <!-- 新增 -->
          <el-button size="medium" type="primary" plain @click="showCreate" v-power="2">新建</el-button>
        </div>
      </table-bar>
      <tao-table
        class="table-box"
        :data="dataList"
        :page="page"
        @changePage="changePage"
        :showPage="true"
        ref="table"
      >
        <!-- 详情 -->
        <el-table-column label="详情" align="center" width="80" type="expand">
          <template slot-scope="scope">
            <info :row="scope.row" />
          </template>
        </el-table-column>
        <!-- ID -->
        <el-table-column v-if="columns[0].show" align="center" width="100" label="ID" prop="id" />
        <!-- 名称 -->
        <el-table-column v-if="columns[1].show" label="名称" prop="userlabel" show-overflow-tooltip />
        <!-- 类型 -->
        <el-table-column
          v-if="columns[3].show"
          align="center"
          width="200"
          label="类型"
          sortable
          prop="type"
        >
          <template
            slot-scope="scope"
          >{{scope.row.type === 'EVPN_OVER_IPSEC' ? 'EVPN Over IPSEC' : 'EVPN Over L2TP'}}</template>
        </el-table-column>
        <!-- 状态 -->
        <el-table-column v-if="columns[2].show" label="状态" align="center" width="100" prop="state">
          <template slot-scope="scope">{{scope.row.state === 'CONNECTED' ? 'UP' : 'DOWN'}}</template>
        </el-table-column>
        <!-- 监控 -->
        <el-table-column
          v-if="columns[3].show"
          align="center"
          width="100"
          label="监控"
          prop="enableSqa"
        >
          <template slot-scope="scope">{{scope.row.enableSqa ? '开启' : '关闭'}}</template>
        </el-table-column>
        <!-- 丢包率 -->
        <el-table-column
          v-if="columns[4].show"
          align="center"
          width="100"
          label="丢包率"
          prop="lossRatio"
        >
          <template slot-scope="scope">{{scope.row.lossRatio}} %</template>
        </el-table-column>
        <!-- 时延 -->
        <el-table-column
          v-if="columns[5].show"
          align="center"
          width="100"
          label="时延"
          prop="delayThreshold"
        >
          <template slot-scope="scope">{{scope.row.delayThreshold}} ms</template>
        </el-table-column>
        <!-- 抖动 -->
        <el-table-column v-if="columns[6].show" align="center" width="100" label="抖动" prop="jitter">
          <template slot-scope="scope">{{scope.row.jitter}} ms</template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column fixed="right" label="操作" align="center" width="80px">
          <!-- 删除 -->
          <template slot-scope="scope">
            <el-button
              type="text"
              style="color: #FA6962"
              icon="el-icon-delete"
              @click="handleDelete(scope)"
              :disabled="!scope.row.operationPermissions"
              v-power="scope.row.dgId === 100000 ? 0 : 2"
            >删除</el-button>
          </template>
        </el-table-column>
      </tao-table>
    </template>
    <template v-if="show === 1">
      <create :tenants="tenants" @handleChange="handleChange($event)" />
    </template>
  </div>
</template>

<script>
import create from './create';
import info from './info';

export default {
  components: {
    info,
    create
  },
  data () {
    return {
      dvEdit: false,
      dialogTitle: '',
      page: {
        page: 1,
        pageSize: 15,
        total: 0
      },
      form: {
        userlabel: null,
        speed: false,
        rateLimit: 0
      },
      dataList: [],
      filterSearch: {
        userlabel: null
      },
      columns: [
        { name: 'ID', show: true },
        { name: '名称', show: true },
        { name: '类型', show: true },
        { name: '状态', show: true },
        { name: '监控', show: true },
        { name: '丢包率', show: true },
        { name: '时延', show: true },
        { name: '抖动', show: true }
      ],
      dialogStr: false,
      samgrList: [],
      tunnelInfo: {},
      show: 0,
      tenants: []
    };
  },
  mounted () {
    let _this = this;
    _this.$nextTick(() => {
      _this.refresh();
    });
  },
  methods: {
    // 回调
    handleChange (val) {
      let _this = this;
      _this.show = val;
    },
    // 搜索
    search () {
      let _this = this;
      _this.page.page = 1;
      _this.refresh();
    },
    // 重置
    reset () {
      let _this = this;
      _this.page.page = 1;
      _this.filterSearch = {
        label: null
      };
      _this.refresh();
    },
    // 刷新
    refresh () {
      let _this = this;
      let params = {
        userlabel: _this.filterSearch.userlabel,
        limit: _this.page.pageSize,
        offset: (_this.page.page - 1) * _this.page.pageSize
      };
      _this.$http.post({
        url: '/api/sdwan/wan-pipeline/v1/query',
        data: params
      }).then(res => {
        let { status, resultBody } = res;
        if (status === '0') {
          _this.page.total = resultBody.count;
          _this.dataList = resultBody.objects;
          _this.$emit('handleRefresh', 'evpn');
        }
      });
    },
    // 过滤显示字段
    changeColumn (columns) {
      this.columns = columns;
      this.$refs.table.doLayout();
    },
    // 切换页码 & 显示条数
    changePage (page) {
      if (page.page) {
        this.page.page = page.page;
      } else {
        this.page.page = 1;
        this.page.pageSize = page.limit;
      }
      this.refresh();
    },
    // 过滤回显
    filterWan (site, ne, wan) {
      let result = [site.sitename, ne.userlabel, wan];
      return result;
    },
    // 新建隧道
    showCreate () {
      let _this = this;
      let arry = [];
      _this.$http.post({
        url: '/api/sdwan/tenant/getMultiDg',
        data: {
          limit: 0,
          offset: 0,
          hasDefault: true
        }
      }).then(res => {
        let { status, resultBody } = res;
        if (status === '0') {
          for (let i = 0; i < resultBody.objects.length; i++) {
            arry.push({
              value: resultBody.objects[i].id,
              label: resultBody.objects[i].userlabel
            });
          }
          _this.tenants = arry;
          _this.show = 1;
        }
      });
    },
    // 删除
    handleDelete (scope) {
      let _this = this;
      this.$confirm('您确定要删除当前数据吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        _this.$http.deleted({ url: '/api/sdwan/wan-pipeline/v1/' + scope.row.id }).then(res => {
          let { status } = res;
          if (status === '0') {
            _this.$message.success('删除成功！');
            _this.refresh();
          }
        });
      }).catch(() => {
        _this.$message.info('取消删除');
      });
    },
  }
};
</script>

<style lang='scss' scoped>
.evpnoipsc-content {
  width: 100%;
  height: 100%;

  .avatar {
    width: 50px;
    height: 50px;
    border-radius: 50%;
  }
  .table-box {
    ::v-deep .el-button {
      padding: 0;
    }
    ::v-deep .el-dropdown-link {
      cursor: pointer;
      color: #409eff;
      margin-left: 5px;
    }
  }

  ::v-deep .el-dialog__body {
    padding-bottom: 0;
  }
}
</style>